package com.ccit.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ccit.common.client.EmployeeClient;
import com.ccit.common.exception.GymException;
import com.ccit.common.result.Result;
import com.ccit.common.utils.Email;
import com.ccit.common.utils.MD5;
import com.ccit.model.Admin;
import com.ccit.model.Employee;
import com.ccit.user.mapper.AdminMapper;
import com.ccit.user.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Transactional(rollbackFor = Exception.class)
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private RedisTemplate redisTemplate;
//    @Autowired
//    private EmployeeClientUser employeeClientUser;
    @Autowired
    private EmployeeClient employeeClient;

    @Override
    public boolean isExist(String email) {
        boolean flag = false;
        //根据email查询该管理员是否存在
        List<Admin> list = adminMapper.isExist(email);
        //根据记录数判断是否存在
        if (list.size() > 0) {
            flag = true;
        }
        return flag;
    }

    @Override
    public Admin selectAdminByEmail(Admin admin) {
        //获取前端传入密码
        String pd = admin.getPassword();
        //将前端传入密码进行md5加密
        String password = MD5.encodeMsg(pd);
        //通过email从数据库中获取该管理员的密码
        Admin correctAdmin = adminMapper.selectAdminByEmail(admin.getEmail());
        String pawd = correctAdmin.getPassword();
        //判断前后端密码是否一致  如果一致返回admin对象，不一致则返回null
        if (password.equals(pawd)) {
            //如果数据一致
            return correctAdmin;
        }
        return null;
    }

    @Override
    public String getCheckCode(String email) throws Exception {
        String code = Email.sendEmail(email);
        redisTemplate.opsForValue().set("checkCode:" + email, code, 300, TimeUnit.SECONDS);
        return code;
    }

    @Override
    public boolean saveEmployee(Employee employee) {
        employee.setType("前台");
        Result result = employeeClient.saveEmployee(employee);
        if (result.getMessage().equals("成功")) {
            return true;
        }
        return false;
    }

    @Override
    public Admin login(Admin admin) {
        String email = admin.getEmail();
        String password = MD5.encodeMsg(admin.getPassword());
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("email", email);
        wrapper.eq("password", password);
        Admin admin1 = adminMapper.selectOne(wrapper);
        if (admin1 != null)
            return admin1;
        else
            throw new GymException(202,"邮箱或密码错误");
    }
}
